Skip to content

Comments

feat: Add Pydantic AI provider with PostHog OTel instrumentation#27

Open
andrewm4894 wants to merge 1 commit intomainfrom
feature/pydantic-ai-provider
Open

feat: Add Pydantic AI provider with PostHog OTel instrumentation#27
andrewm4894 wants to merge 1 commit intomainfrom
feature/pydantic-ai-provider

Conversation

@andrewm4894
Copy link
Member

Summary

  • Add PydanticAIProvider using Pydantic AI's native OpenTelemetry instrumentation
  • Integrate with posthog-python's new instrument_pydantic_ai() function (from upcoming PR)
  • Support multi-turn conversations via message history
  • Include get_weather and tell_joke tool examples
  • Add pydantic-ai to requirements.txt

How it works

Pydantic AI has native OTel instrumentation via Agent.instrument_all(). The posthog-python SDK provides a custom SpanExporter that translates these OTel spans into PostHog $ai_generation and $ai_span events.

from posthog.ai.pydantic_ai import instrument_pydantic_ai

# One-liner to set up PostHog instrumentation
instrument_pydantic_ai(posthog_client, distinct_id="user_123")

# Use Pydantic AI agents normally - automatically traced
agent = Agent('openai:gpt-4')
result = await agent.run('Hello')

Dependencies

This PR depends on a corresponding posthog-python PR that adds the OTel exporter and instrument_pydantic_ai() function.

Test plan

  • Tested with local posthog-python branch
  • Verified events appear correctly in PostHog LLM Analytics UI
  • Tested tool calling (get_weather, tell_joke)
  • Tested multi-turn conversations

🤖 Generated with Claude Code

- Add PydanticAIProvider using Pydantic AI's native OTel instrumentation
- Integrate with posthog-python's new instrument_pydantic_ai() function
- Support multi-turn conversations via message history
- Include get_weather and tell_joke tool examples
- Add pydantic-ai to requirements.txt

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@andrewm4894 andrewm4894 self-assigned this Dec 2, 2025
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +61 to +65
try:
from posthog.ai.pydantic_ai import instrument_pydantic_ai
except ImportError:
raise ImportError(
"PostHog pydantic-ai integration is required. "

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Pydantic provider always fails due to missing PostHog module

_setup_instrumentation imports posthog.ai.pydantic_ai.instrument_pydantic_ai and raises immediately if it is absent, but python/requirements.txt still pins posthog>=6.6.1, which does not ship that module (the commit text even calls it an “upcoming PR”). With the current dependency set, selecting provider 13 or running run_all_tests will hit this ImportError and the provider cannot be used at all. Pin PostHog to a release that contains the pydantic AI instrumentation or guard the provider until that version is available.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant